package com.tengju.bff.interfaces.manage.platform.rule;

import com.tengju.bff.interfaces.manage.platform.rule.assembler.RuleAssembler;
import com.tengju.bff.interfaces.manage.platform.rule.form.EditFullMatchRuleVO;
import com.tengju.bff.interfaces.manage.platform.rule.form.FullMatchRuleVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.support.application.rule.RuleService;
import com.tengju.support.domain.model.rule.FullMatchRuleForm;
import com.tengju.user.domain.model.user.Staff;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@Api(tags = "平台运营-规则管理")
@RequestMapping(value = "manage/platform/rule/")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class RuleController {

    private final RuleService ruleService;

    @ApiOperation(value = "添加全匹配规则", notes = "添加全匹配规则")
    @RequestMapping(value = "fullMatch/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> add(@Valid @RequestBody FullMatchRuleVO form) {
        Staff staff = ManageUtil.getOperateUser();
        FullMatchRuleForm fullMatchRuleForm = RuleAssembler.toModel(form);
        ruleService.registerRule(fullMatchRuleForm,staff.getName());
        return ApiResponse.newSuccess();
    }

    @ApiOperation(value = "变更全匹配规则", notes = "变更全匹配规则")
    @RequestMapping(value = "fullMatch/edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> edit(@Valid @RequestBody EditFullMatchRuleVO form) {
        Staff staff = ManageUtil.getOperateUser();
        FullMatchRuleForm fullMatchRuleForm = RuleAssembler.toModel(form);
        ruleService.registerRule(fullMatchRuleForm,staff.getName());
        return ApiResponse.newSuccess();
    }

}
